package keter.combofs.config;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 S3配置
 */
public class S3Config {
	@Value("${combofs.s3.endpoint:any}")
	protected String endpoint;
	@Value("${combofs.s3.accessKey:any}")
	protected String accessKey;
	@Value("${combofs.s3.secretKey:any}")
	protected String secretKey;

	private AmazonS3 amazonS3;

	@Autowired
	public void init(){
		AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
		ClientConfiguration clientConfig = new ClientConfiguration();
		clientConfig.setProtocol(Protocol.HTTP);
		AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials));
		AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(endpoint, Regions.DEFAULT_REGION.getName());
		builder.setEndpointConfiguration(endpointConfiguration);
		amazonS3 =  builder.build();
	}

	public AmazonS3 client(){
		return amazonS3;
	}

}